//
//  YSTabbar.swift
//  新浪浪
//
//  Created by mac on 16/2/26.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

class YSTabbar: UITabBar {

                                                    // 自定义UI控件的3⃣️的步骤:
                                                    //      1⃣️重写构造方法(init...)
                                                    //          2⃣️私有方法
        //1⃣️                                        //              3⃣️懒加载
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
        
    }

            //自动生成提醒不用管!!!!
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
        //2⃣️
    private func setUI(){
        addSubview(composeButton)
    }
    
    
        //3⃣️懒加载 : compose:重新构建composeButton(中心加号按钮)
    lazy var composeButton: UIButton = {
        let button = UIButton (type: UIButtonType.Custom)
        
        button .setBackgroundImage(UIImage (named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button .setBackgroundImage(UIImage (named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        button .setImage(UIImage (named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button .setImage(UIImage (named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        
        
        //有返回值 相当于重写get方法
        return button
    }()
    
    
    
    /*******************************************/
    /*      在这里布局 这5个button的frame     */
    /***************************************************/
    override func layoutSubviews() {
        
        var index = 0
        let w = bounds.size.width/5
        let h = bounds.height
//        let x = CGFloat (index++) * w
        let y = CGFloat (0)
        /*
            布局其余三个
        */
        for subview in subviews{
            if ( subview .isKindOfClass(NSClassFromString("UITabBarButton")!) ){
                
                subview.frame = CGRectMake(CGFloat(index++)*w, y, w, h)
                
                if index == 2 {
                    index++
                }
                
            }
            /*
                布局中间一个
            */
            composeButton.frame = CGRectMake(2 * w, y, w, h )
        }
        
    }
    
    
    

}











